<?php
if(isset($_REQUEST['akse_get_data'])){
	session_start();

	$nip=$_SESSION['nip'];
	
	echo json_encode($nip);

}

if(isset($_REQUEST['aksi_simpan_usulan'])){
	require_once("../class/class.usulan_peg.php");

	$nip=$_REQUEST['nip'];
	$tglAwal=$_REQUEST['tglAwal'];
	$tglAkhir=$_REQUEST['tglAkhir'];
	$id_cuti=$_REQUEST['id_cuti'];
	$id_tujuan=$_REQUEST['id_tujuan'];
	$nipAtasan=$_REQUEST['nipAtasan'];


	$usulanPeg= new UsulanPeg();

	$usulanPeg->nip=$nip;
	$usulanPeg->tglAwal=$tglAwal;
	$usulanPeg->tglAkhir=$tglAkhir;
	$usulanPeg->id_cuti=$id_cuti;
	$usulanPeg->id_tujuan=$id_tujuan;
	$usulanPeg->nipAtasan=$nipAtasan;

	$msg=false;
	$error="Gagal rekam usulan";

	$usulanDao= new UsulanPegDao();
	$insert=$usulanDao->createUsulan($usulanPeg);

	if($insert){
		$msg=true;
	}

	$data=array(
		'msg' => $msg,
		'error' => $error,
		);

	echo json_encode($data);
}

if(isset($_REQUEST['aksi_simpan_ubah_usulan'])){
	require_once("../class/class.usulan_peg.php");

	$id=$_REQUEST['id'];

	$nip=$_REQUEST['nip'];
	$tglAwal=$_REQUEST['tglAwal'];
	$tglAkhir=$_REQUEST['tglAkhir'];
	$id_cuti=$_REQUEST['id_cuti'];
	$id_tujuan=$_REQUEST['id_tujuan'];
	$nipAtasan=$_REQUEST['nipAtasan'];

	$usulanPeg= new UsulanPeg();
	$usulanPeg->id=$id;
	$usulanPeg->nip=$nip;
	$usulanPeg->nipAtasan=$nipAtasan;
	$usulanPeg->tglAwal=$tglAwal;
	$usulanPeg->tglAkhir=$tglAkhir;
	$usulanPeg->id_cuti=$id_cuti;
	$usulanPeg->id_tujuan=$id_tujuan;

	$msg=false;
	$error="Gagal ubah usulan";

	$usulanDao= new UsulanPegDao();
	$insert=$usulanDao->updateUsulan($usulanPeg);

	if($insert){
		$msg=true;
	}

	$data=array(
		'msg' => $msg,
		'error' => $error,
		);

	echo json_encode($data);
}

if(isset($_REQUEST['aksi_hapus_usulan'])){
	require_once("../class/class.usulan_peg.php");

	$id=$_REQUEST['id'];	

	$msg=false;
	$error="Gagal hapus usulan";

	$usulanDao= new UsulanPegDao();
	$del=$usulanDao->deleteUsulan($id);

	if($del){
		$msg=true;
	}

	$data=array(
		'msg' => $msg,
		'error' => $error,
		);

	echo json_encode($data);
}

if(isset($_REQUEST['akse_get_data_ubah'])){
	require_once("../class/class.usulan_peg.php");

	$id=$_REQUEST['id'];
	$usulanPegDao= new UsulanPegDao();
	$msg=false;
	$usulan=$usulanPegDao->getById($id);
	if($usulan!=false){
		$msg=true;
	}

	$data=array(
		'msg' => $msg,
		'usulan' => $usulan,
		);

	echo json_encode($data);
}

if(isset($_REQUEST['aksi_get_usulan'])){
	require_once("../class/class.usulan_peg.php");
	require_once("../class/class.refcuti.php");
	require_once("../class/class.tujuan.php");

	session_start();

	$nip=$_SESSION['nip'];
	$usulanDao= new UsulanPegDao();

	$usulan=$usulanDao->getAll($nip);
	$tujuanDao= new TujuanDao();
	$arr=array();
	$refDao= new RefCutiDao();
	$msg=false;
	$arr=array();
	if($usulan!=false){
		$msg= true;
		for($i=0;$i<count($usulan);$i++){
			$arr[$i]=array(
				'usulan' => $usulan[$i],
				'ref' => $refDao->getCuti($usulan[$i]['id_cuti']),
				'tujuan' =>$tujuanDao->getTujuan($usulan[$i]['id_tujuan']),
				);
		}
	}
	$data=array(
		'msg' => $msg,
		'usulan' => $arr,
		);

	echo json_encode($data);
}

if(isset($_REQUEST['aksi_usulan_cetak'])){
	if($_REQUEST['aksi_usulan_cetak']=="cetak"){

		include_once('../class/tbs_class.php');
		include_once('../class/plugins/tbs_plugin_opentbs.php');
		require_once("../class/class.usulan_peg.php");
		require_once("../class/class.refcuti.php");
		require_once("../class/class.tujuan.php");
		require_once("../class/class.pegawai.php");
		require_once("../class/class.golongan.php");
		require_once("../class/lib.tanggal.php");

		$TBS = new clsTinyButStrong;
		$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);

		$id=$_POST['id_usulan_cetak'];
		$usulanPegDao= new UsulanPegDao();
		$refCutiDao= new RefCutiDao();
		$tujuanDao= new TujuanDao();
		$tanggal= new Tanggal();
		$pegawaiDao=new PegawaiDao();
		$golDao= new GolonganDao();

		$usulan= $usulanPegDao->getById($id);
		$tglSkrg=$tanggal->formatTanggalTanpaHari(date('Y-m-d'));

		$nip=$usulan['nip'];
		$nipAtasan=$usulan['nipAtasan'];
		$tglAwal=$tanggal->formatTanggalTanpaHari($usulan['tglAwal']);
		$tglAkhir=$tanggal->formatTanggalTanpaHari($usulan['tglAkhir']);

		$pegawai= $pegawaiDao->getPegawai($nip);

		$nama=$pegawai['nama'];
		$kdgol=$pegawai['gol'];
		$golongan=$golDao->getGolongan($kdgol);

		$gol=$golongan['nmgol'];
		$pangkat=$golongan['pangkat'];
		$idJnsCuti=$usulan['id_cuti'];
		$idTujuan=$usulan['id_tujuan'];

		$tujuan=$tujuanDao->getTujuan($idTujuan);
		$refCuti=$refCutiDao->getCuti($idJnsCuti);

		$unit=$pegawai['unit'];
		$jabatan=$pegawai['eselon'];

		$atasan=$pegawaiDao->getPegawai($nipAtasan);

		$namaAtasan=$atasan['nama'];

		$TBS->LoadTemplate('../assets/permohonan.docx');

		$file_name = 'permohonan'.'_'.$pegawai['nama'].'_'.date('Y-m-d');

		$TBS->Show(OPENTBS_DOWNLOAD, $file_name . '.docx');

	}
}
?>